<?php
//header('charset:utf8');
header("Content-type:text/html;charset=utf-8");
$link = mysqli_connect('localhost','root','');
$ret = mysqli_select_db($link,'overheads');
include 'simple_html_dom.php';

mysqli_query($link,'SET NAMES UTF8');
$url = "http://www.ttmeishi.com/YuanLiao";
//$html = file_get_html($url);

// 查找class=quizPutTag的div，也可以根据div的id获取
/*
$type_fill = $html->find('#content h2.yl_content_h2');
foreach ($type_fill as $r) {
    //一级分类
    $name = $r->children(0)->innertext;
    $href = $r->children(0)->href;
    $href = 'http://www.ttmeishi.com'.$href;

    $name=iconv("GBK", "UTF-8", $name);
    //echo $name.'<br />';
    //$name = iconv('gbk','utf-8',$name);

    //echo $first_cat.'==='.$first_inner.'<br />';

    $second_cats = $html->find('div.yl_leibie div.yl_leibie_t strong');
    foreach($second_cats as $r2){
        $cat2 = $r2->children(0)->innertext;
        echo $cat2.'<br />';
    }
    echo '<hr />';

    $sqlString = "INSERT INTO overheads.oh_cates(`id`,`url`,`name`)VALUES(null,'{$href}','{$name}');";
    $r = mysqli_query($link,$sqlString);
    if(!$r) exit('error');
}
$html->clear();
*/


$sqlString = "select * from oh_cates";
$res = mysqli_query($link,$sqlString);

while($row = mysqli_fetch_assoc($res)){
    $url = trim($row['url']);
    $html = file_get_html($url);
    $type_fill = $html->find('div.yl_leibie');
    echo $row['name'].'<br />';
    foreach($type_fill as $v){
        $second_cat = $v->children(0)->children(0)->children(0)->innertext;
        $second_cat = iconv("GBK", "UTF-8", $second_cat);
        echo '----'.$second_cat.'<br />';

        $sqlString = "INSERT INTO overheads.oh_cates(`id`,`url`,`name`,pid)VALUES(null,' ','{$second_cat}',{$row['id']});";

        $r = mysqli_query($link,$sqlString);
        $pid = mysqli_insert_id($link);

        //获取具体菜名
       $alist = $v->children(1)->find('li a');
       foreach($alist as $a){
           $cat3 = iconv("GBK", "UTF-8", $a->innertext);
           echo '--------'.$cat3.'<br />';

           $sqlString = "INSERT INTO overheads.oh_cates(`id`,`url`,`name`,pid)VALUES(null,'','{$cat3}',{$pid});";
           $r = mysqli_query($link,$sqlString);
       }
       echo '<hr />';
    }
    $html->clear();
}

